<?php
/**
 * Cms_Allocation
 *
 * @category   Cms
 * @package    Cms_Model
 */
class Cms_Model_Allocation extends BIS_Model_Abstract
{

    /**
     * Get Allocation by ID
     *
     * @param int $allocationId The allocation id
     * @return Cms_Resource_Allocation_Item|null
     */
    public function getAllocationById($allocationId)
    {
        $allocationId = (int) $allocationId;

        return $this->getResource('Allocation')->getAllocationById($allocationId);
    }

     /**
     * Get list of Allocations
     *
     * @param null|array  $data  sort, order, page, limit
     * @return Zend_Db_Table_Rowset|Zend_Paginator
     */
    public function getAllocations($data = null)
    {
        if ($data) {

			$sortData = array('dateAllocated','allocatedBy');

                        $order = '';
			if (isset($data['sort']) && in_array($data['sort'], $sortData)) {
				$order .= $data['sort'];
			} else {
				$order .= "dateAllocated";
			}

			if (isset($data['order']) && ($data['order'] == 'desc')) {
				$order .= " DESC";
			} else {
				$order .= " ASC";
			}

                        $data['order'] = $order;

			if (isset($data['page'])) {
				if ($data['page'] < 0) {
					$data['page'] = 0;
				}
			}

                        if (isset($data['limit'])) {
				if ($data['limit'] < 1) {
					$data['limit'] = 20;
				}
			}else{
                            $data['limit'] = 20;
                        }

			return $this->getResource('Allocation')->getAllocations($data);

		} else {
			return $this->getResource('Allocation')->getAllocations();
			}
    }


    /**
     * Get Allocation Products by allocation id
     *
     * @param int $allocationId The allocation id to search for
     * @return Zend_Db_Table_Rowset
     */
    public function getAllocationProducts($allocationId)
    {
        $allocationId = (int) $allocationId;

        return $this->getResource('AllocationProduct')->getAllocationProducts($allocationId);
    }

    /**
     * Get Total sum of a product allocated to branches
     *
     * @param int     $productId Product Id
     * @return Zend_Db_Table_Rowset
     */
    public function getTotalBranchesAllocationByProductId($productId)
    {
        $productId = (int) $productId;

        return $this->getResource('AllocationProduct')->getTotalBranchesAllocationByProductId($productId);
    }

    /**
     * Get total quantity of a product allocated to a branch
     *
     * @param int      $productId Product Id
     * @param int      $branchId Branch Id
     * @return int     Total quantity of the product allocated to a branch
     */
    public function getTotalBranchAllocationByProductId($productId, $branchId)
    {
     
        $productId = (int) $productId;
        $branchId = (int) $branchId;

        return $this->getResource('AllocationProduct')->getTotalBranchAllocationByProductId($productId, $branchId);
    }

    /**
     * Save Allocation
     *
     * @param string string $allocatedBy The full name of the administrator that allocated the products
     * @return mixed The primary key
     */
    public function saveAllocation($allocatedBy)
    {
        $data = array();
        $data['dateAllocated'] = date('Y-m-d H:i:s');
        $data['allocatedBy'] = $allocatedBy;

        return $this->getResource('Allocation')->saveRow($data);
    }

    /**
     * Save Allocation Product
     *
     * @param array  $data The data to insert/update
     * @return mixed The primary key
     */
    public function saveAllocationProduct($data)
    {
        return $this->getResource('AllocationProduct')->saveRow($data);
    }

    /**
     * Get allocation report
      *
     * @param int|null   $productId   product id
     * @param int|null   $branchId    branch id
     * @param array      $data fromDate, toDate
     * @return Zend_Db_Table_Rowset
     */
    public function getAllocationReport($productId=null, $branchId=null, $data = array()){

        return $this->getResource('AllocationProduct')->getAllocationReport($productId, $branchId, $data);

    }
}